package com.sxhuangjiang.litadmin.config.datasource;

import com.sxhuangjiang.litadmin.utils.AESUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @author huang
 * @version V1.0
 * @date 2019-11-08 15:44
 */
@Configuration
public class DruidDataSourceConfig {
    @Autowired
    DataSourceProperties properties;

    /**
     * 自定义处理数据源的配置
     * 此处是为了解密配置文件中的url地址和密码等信息。
     * @return
     */
    @Bean
    public DataSource getDruidDataSource(){
        String url = properties.getUrl();
        properties.setUrl(AESUtil.decrypt(url,AESUtil.KEY));
        String password = properties.getPassword();
        properties.setPassword(AESUtil.decrypt(password,AESUtil.KEY));
        return properties.initializeDataSourceBuilder().build();
    }

}
